{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solución: tiempo esperado de quiebra\n",
    "Utilizamos el `choiceWeighted` ya resuelto."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def choiceWeighted(dist):\n",
    "    probTotal = sum(dist.values())\n",
    "    sorteo = random.uniform(0, probTotal)\n",
    "    acum = 0\n",
    "    for (val, prob) in dist.items():\n",
    "        acum += prob\n",
    "        if acum >= sorteo:\n",
    "           return val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Codificamos el grafo de transición con un diccionario de diccionarios\n",
    "transicEsq1 = dict(\n",
    "    A=dict(A=0.5, B=0.2, D=0.3),\n",
    "    B=dict(B=0.6, C=0.3, D=0.1),\n",
    "    C=dict(C=0.9, E=0.1),\n",
    "    D=dict(D=0.6, C=0.1, H=0.3),\n",
    "    E=dict(E=0.5, F=0.3, Q=0.2),\n",
    "    F=dict(F=0.8, I=0.1, Q=0.1),\n",
    "    G=dict(G=0.5, A=0.2, D=0.2, H=0.1),\n",
    "    H=dict(H=0.7, E=0.2, I=0.1),\n",
    "    I=dict(I=0.5, J=0.3, Q=0.2),\n",
    "    J=dict(J=0.6, H=0.3, Q=0.1),\n",
    "    Q=dict(Q=1),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def tiempoQuiebra(transic, estadoInic, estadoQuieb):\n",
    "    steps = 0\n",
    "    est = estadoInic\n",
    "    while est != estadoQuieb:\n",
    "          steps += 1\n",
    "          est = choiceWeighted(transic[est])\n",
    "    return steps\n",
    "\n",
    "\n",
    "def estimProbsQuiebra(transic, estadoQuieb, nSims=1000):\n",
    "    estim = {}\n",
    "    for est in transic:\n",
    "        steps = 0\n",
    "        for _ in range(nSims):\n",
    "            steps += tiempoQuiebra(transic, est, estadoQuieb)\n",
    "        estim[est] = steps / nSims\n",
    "    return estim\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "estimProbsQuiebra(transicEsq1, 'Q', nSims=10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Este problema es un problema conocido en *cadenas de Markov*: el tiempo esperado de llegada a un\n",
    "*estado absorvente*. Tiene una solución algebraica exacta, que veremos en el módulo de programación científica."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
